import {HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Injectable} from "@angular/core";
import {AuthService} from "../public/service/auth-service";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // Get the auth token from the service.
    const authToken = AuthService.getAuthorizationToken();
    const defaultHeader = {ContentType: 'Application-json'};
    const authHeader = {Authorization: authToken};

    const headers = {
      ...defaultHeader,
      ...authHeader
    };

    // Clone the request and replace the original headers with
    // cloned headers, updated with the new defined headers.
    const authReq = req.clone({ setHeaders: headers });

    // send cloned request with header to the next handler.
    return next.handle(authReq);
  }
}
